import {createWebHistory, createRouter} from 'vue-router'

import LoginView from '@/views/login.vue'
import ChangePasswordView from '@/views/changePassword.vue'
import DashboardLayoutView from '@/components/layout/DashboardLayout.vue'
import App from "@/App.vue";
import {$localStorage} from "@/utils/application-storage.js";
import ApplicationMntLayoutView from "@/components/layout/ApplicationMntLayoutView.vue";
import ApplicationSettingLayoutView from "@/components/layout/ApplicationSettingLayoutView.vue";


const routes = [
    {
        path: '/',
        redirect: '/:tenantCode/login'
    },
    {
        path: '/:tenantCode',
        component: App,
        redirect: '/:tenantCode/login',
        children: [
            {
                path: 'login',
                name: 'Login',
                component: LoginView,
            },
            {
                path: 'change-password',
                name: 'ChangePassword',
                component: ChangePasswordView,
            },
            {
                path: 'dashboard',
                name: 'Home',
                component: DashboardLayoutView,
                redirect: 'dashboard/workspace',
                children: [
                    {
                        path: 'workspace',
                        name: 'DashboardWorkSpace',
                        component: () => import('@/views/Dashboard/Workspace.vue'),
                        meta: {
                            title: '工作台',
                            logo: 'iconchaosongwode2',
                            code: 'workbench',
                        },
                    },
                    {
                        path: 'app-mnt',
                        name: 'DashboardAppMnt',
                        component: () => import('@/views/Dashboard/AppMnt.vue'),
                        meta: {
                            title: '应用管理',
                            logo: 'iconchaosongwode2',
                            code: 'workbench',
                        },
                    },
                    {
                        path: 'workflow-mnt',
                        name: 'DashboardWorkflowMnt',
                        component: () => import('@/views/Dashboard/WorkflowMnt.vue'),
                        meta: {
                            title: '流程管理',
                            logo: 'iconchaosongwode2',
                            code: 'workbench',
                        },
                        children: [
                            {
                                path: 'workflow-design',
                                name: 'WorkFlowDesign',
                                component: () => import('@/views/WorkflowMnt/WorkFlowDesign.vue'),
                                meta: {
                                    title: '流程设计',
                                    logo: 'iconchaosongwode2',
                                    code: 'workbench',
                                },
                            },
                            {
                                path: 'workflow-instance',
                                name: 'WorkFlowInstance',
                                component: () => import('@/views/WorkflowMnt/WorkFlowInstance.vue'),
                                meta: {
                                    title: '流程实例',
                                    logo: 'iconchaosongwode2',
                                    code: 'workbench',
                                },
                            },
                            {
                                path: 'history-task',
                                name: 'HistoryTask',
                                component: () => import('@/views/WorkflowMnt/HistoryTask.vue'),
                                meta: {
                                    title: '历史任务',
                                    logo: 'iconchaosongwode2',
                                    code: 'workbench',
                                },
                            },
                            {
                                path: 'history-workflow',
                                name: 'HistoryWorkflow',
                                component: () => import('@/views/WorkflowMnt/HistoryWorkflow.vue'),
                                meta: {
                                    title: '历史流程',
                                    logo: 'iconchaosongwode2',
                                    code: 'workbench',
                                },
                            },
                        ]
                    }
                ]
            },
            {
                path: 'application-detail/:applicationId',
                name: 'ApplicationDetail',
                component: ApplicationMntLayoutView,
                children: [
                    {
                        path: 'test-page',
                        name: 'ApplicationDetailTestPage',
                        component: () => import('@/views/Application/TestPage.vue'),
                        meta: {
                            title: '应用管理',
                            logo: 'iconchaosongwode2',
                            code: 'application_management',
                        },
                        children: []
                    },
                ]
            },
            {
                path: 'application-admin-detail/:applicationId',
                name: 'ApplicationAdminDetail',
                component: ApplicationMntLayoutView,
                children: [
                    {
                        path: 'test-page',
                        name: 'ApplicationAdminDetailTestPage',
                        component: () => import('@/views/Application/TestPage.vue'),
                        meta: {
                            title: '应用管理',
                            logo: 'iconchaosongwode2',
                            code: 'application_management',
                        },
                        children: []
                    },
                ]
            },
            {
                path: 'application-setting/:applicationId',
                name: 'ApplicationSetting',
                component: ApplicationSettingLayoutView,
                meta: {
                    title: '应用设置',
                    logo: 'iconchaosongwode2',
                    code: 'application_management',
                },
                children: [
                    {
                        path: 'basic-info',
                        name: 'ApplicationBasicInfo',
                        component: () => import('@/views/Application/Setting/BasicInfo.vue'),
                        meta: {
                            title: '应用设置的基本信息',
                            logo: 'iconchaosongwode2',
                            code: 'application_management',
                        },
                    },
                    {
                        path: 'menu',
                        name: 'ApplicationSettingMenu',
                        component: () => import('@/views/Application/Setting/Menu.vue'),
                        meta: {
                            title: '应用设置的菜单管理',
                            logo: 'iconchaosongwode2',
                            code: 'application_management',
                        },
                    },
                    {
                        path: 'online-data-table',
                        name: 'ApplicationSettingOnlineDataTablePage',
                        component: () => import('@/views/Application/Setting/OnlineDataTable.vue'),
                        meta: {
                            title: '应用设置的在线数据表',
                            logo: 'iconchaosongwode2',
                            code: 'application_management',
                        },
                    },
                    {
                        path: 'system-service',
                        name: 'ApplicationSettingSystemServicePage',
                        component: () => import('@/views/Application/Setting/SystemService.vue'),
                        meta: {
                            title: '应用设置的系统服务',
                            logo: 'iconchaosongwode2',
                            code: 'application_management',
                        },
                    },
                    {
                        path: 'view-management',
                        name: 'ApplicationSettingViewMntPage',
                        component: () => import('@/views/Application/Setting/ViewMnt.vue'),
                        meta: {
                            title: '应用设置的视图管理',
                            logo: 'iconchaosongwode2',
                            code: 'application_management',
                        },
                    },
                    {
                        path: 'form-management',
                        name: 'ApplicationSettingFormMntPage',
                        component: () => import('@/views/Application/Setting/FormMnt.vue'),
                        meta: {
                            title: '应用设置的表单管理',
                            logo: 'iconchaosongwode2',
                            code: 'application_management',
                        },
                    },
                    {
                        path: 'dict-management',
                        name: 'ApplicationSettingDictMntPage',
                        component: () => import('@/views/Application/Setting/DictMnt.vue'),
                        meta: {
                            title: '应用设置的字典管理',
                            logo: 'iconchaosongwode2',
                            code: 'application_management',
                        },
                    },
                    {
                        path: 'template-import-management',
                        name: 'ApplicationSettingTemplateImportMntPage',
                        component: () => import('@/views/Application/Setting/TemplateImportMnt.vue'),
                        meta: {
                            title: '应用设置的导入模板管理',
                            logo: 'iconchaosongwode2',
                            code: 'application_management',
                        },
                    },
                    {
                        path: 'template-export-management',
                        name: 'ApplicationSettingTemplateExportMntPage',
                        component: () => import('@/views/Application/Setting/TemplateExportMnt.vue'),
                        meta: {
                            title: '应用设置的导出模板管理',
                            logo: 'iconchaosongwode2',
                            code: 'application_management',
                        },
                    },
                ]
            }
        ],
    },

]

const router = createRouter({
    history: createWebHistory('/epms2-o/'), // 与 vite.config.js 中的 base 保持一致
    routes,
})

router.beforeEach((to, from, next) => {
    // 从当前路由的路径中提取 tenantCode
    const {tenantCode} = to.params;
    if (tenantCode && tenantCode !== ':tenantCode') {
        // 将 tenantCode 保存到 localStorage 中
        console.log("将 tenantCode 保存到 localStorage 中")
        $localStorage.setTenantCode(tenantCode);
    }
    // 继续路由跳转
    if (to.name !== 'Login' && !$localStorage.getToken()) {
        next({
            path: '/' + tenantCode + '/login',
        })
    }
    next();
});

export default router;
